package com.qd.qx.ui.module2;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qd.common.base.BaseActivity;
import com.qd.common.baseapp.AppManager;
import com.qd.common.baserx.RxSubscriber;
import com.qd.common.commonutils.JsonUtils;
import com.qd.common.commonutils.ToastUitl;
import com.qd.qx.R;
import com.qd.qx.api.Api;
import com.qd.qx.api.ApiServerConstants;
import com.qd.qx.bean.MessageInfoBean;
import com.qd.qx.bean.PeopleInfoBean;
import com.qd.qx.bean.UserData;
import com.qd.qx.ui.main.activity.ChatActivity;
import com.qd.chatui.util.MyUtils;
import com.qd.qx.utils.PhoneUtils;
import com.wevey.selector.dialog.DialogOnItemClickListener;
import com.wevey.selector.dialog.NormalSelectionDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Code is far away from bug with the animal protecting
 * Author：YS on 2018/10/29
 * Function：个人信息
 */
public class PeopleInfoActivity extends BaseActivity {
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvMore)
    TextView tvMore;
    @BindView(R.id.ivMore)
    ImageView ivMore;
    @BindView(R.id.tvHeaderName)
    TextView tvHeaderName;
    @BindView(R.id.tvHeaderBumen)
    TextView tvHeaderBumen;
    @BindView(R.id.tvHeaderImg)
    ImageView tvHeaderImg;
    @BindView(R.id.tvBumen)
    TextView tvBumen;
    @BindView(R.id.llBumen)
    LinearLayout llBumen;
    @BindView(R.id.tvShangji)
    TextView tvShangji;
    @BindView(R.id.llShangji)
    LinearLayout llShangji;
    @BindView(R.id.tvShouji)
    TextView tvShouji;
    @BindView(R.id.llShouji)
    LinearLayout llShouji;
    @BindView(R.id.tvZuoji)
    TextView tvZuoji;
    @BindView(R.id.llZuoji)
    LinearLayout llZuoji;
    @BindView(R.id.tvChuanzhen)
    TextView tvChuanzhen;
    @BindView(R.id.llChuanzhen)
    LinearLayout llChuanzhen;
    @BindView(R.id.tvEmail)
    TextView tvEmail;
    @BindView(R.id.llEmail)
    LinearLayout llEmail;
    @BindView(R.id.tvHRBP)
    TextView tvHRBP;
    @BindView(R.id.llHRBP)
    LinearLayout llHRBP;
    @BindView(R.id.tvDizhi)
    TextView tvDizhi;
    @BindView(R.id.llDizhi)
    LinearLayout llDizhi;
    @BindView(R.id.tvCallPhone)
    TextView tvCallPhone;
    @BindView(R.id.idChat)
    TextView idChat;
    private String otherLoginName;//查询用户的账号
    private String isContains = "0";//是否已经是常用联系人(1-是,0-否)
    private String chatName = "";//聊天名字
    private String realName = "";//真实名字
    private String headerImg = "";//用户头像

    /**
     * 入口
     */
    public static void startAction(Activity activity, String refCode) {
        Intent intent = new Intent(activity, PeopleInfoActivity.class);
        intent.putExtra("otherLoginName", refCode);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                com.qd.common.R.anim.fade_out);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_peopleinfo;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        otherLoginName = getIntent().getStringExtra("otherLoginName");
        tvTitle.setText("名片");
        ivMore.setBackgroundResource(R.drawable.gengduo);

        requestAction(UserData.getInstance().getRefCode(), otherLoginName);
    }

    @OnClick({R.id.ivBack, R.id.ivMore, R.id.tvHeaderImg, R.id.llBumen, R.id.llShangji, R.id.llShouji, R.id.llZuoji, R.id.llChuanzhen, R.id.llEmail, R.id.llHRBP, R.id.llDizhi, R.id.tvCallPhone, R.id.idChat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.ivMore:
                saveAction();
                break;
            case R.id.tvHeaderImg:
                break;
            case R.id.llBumen:
                break;
            case R.id.llShangji:
                break;
            case R.id.llShouji:
                break;
            case R.id.llZuoji:
                break;
            case R.id.llChuanzhen:
                break;
            case R.id.llEmail:
                break;
            case R.id.llHRBP:
                break;
            case R.id.llDizhi:
                break;
            case R.id.tvCallPhone:
                String phone = tvShouji.getText().toString();
                String zuoji = tvZuoji.getText().toString();
                callPhoneAction(phone, zuoji);
                break;
            case R.id.idChat:
                AppManager.getAppManager().finishActivity(TongxunluChildActivity.class);
                ChatActivity.startAction(PeopleInfoActivity.this, -1, chatName + "@" + ApiServerConstants.IM_URL, realName);
                finish();
                break;
        }
    }

    private void callPhoneAction(final String phoneNum, final String zuojiNum) {
        ArrayList<String> list = new ArrayList<>();
        if (!"无".equals(phoneNum)) {
            list.add(phoneNum);
        }
        if (!"无".equals(zuojiNum)) {
            list.add(zuojiNum);
        }
        if (0 == list.size()) {
            ToastUitl.showShort("暂无可拨打电话号码");
            return;
        }
        NormalSelectionDialog selectionDialog = new NormalSelectionDialog.Builder(PeopleInfoActivity.this)
                .setOnItemListener(new DialogOnItemClickListener() {
                    @Override
                    public void onItemClick(Button button, int position) {
                        PhoneUtils.callPhone(PeopleInfoActivity.this, button.getText().toString());
                    }
                }).build();
        selectionDialog.setDataList(list);

        selectionDialog.show();
    }

    private void saveAction() {
        ArrayList<String> list = new ArrayList<>();
        int itemColor = Color.parseColor("#3396fb");
        if ("0".equals(isContains)) {
            list.add("保存常用联系人");
        } else if ("1".equals(isContains)) {
            list.add("移除常用联系人");
            itemColor = Color.parseColor("#ec4c4b");//红色
        }

        NormalSelectionDialog selectionDialog = new NormalSelectionDialog.Builder(PeopleInfoActivity.this)
                .setItemTextColor(itemColor)
                .setOnItemListener(new DialogOnItemClickListener() {
                    @Override
                    public void onItemClick(Button button, int position) {
                        if ("0".equals(isContains)) {
                            addAction(UserData.getInstance().getRefCode(), otherLoginName);
                        } else if ("1".equals(isContains)) {
                            deleteAction(UserData.getInstance().getRefCode(), otherLoginName);
                        }

                    }
                }).build();
        selectionDialog.setDataList(list);

        selectionDialog.show();
    }

    /**
     * 查询用户个人信息
     *
     * @param refCode
     * @param otherLoginName
     */
    private void requestAction(String refCode, String otherLoginName) {
        Map<String, String> map = new HashMap<>();
        map.put("userCode", refCode);
        map.put("contacts", otherLoginName);
        mRxManager.add((Api.getDefault().requestPeopleInfo(Api.newRequestBody(map)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<MessageInfoBean>(PeopleInfoActivity.this, true) {
                    @Override
                    protected void _onNext(MessageInfoBean messageInfoBean) {
                        PeopleInfoBean peopleInfoBean = (PeopleInfoBean) JsonUtils.ObjectToBean(messageInfoBean.getData(), PeopleInfoBean.class);
                        if (null == peopleInfoBean) {
                            ToastUitl.showShort("数据有误");
                            return;
                        }
                        setData(peopleInfoBean);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUitl.showShort("请求失败" + message);
                    }
                }));
    }

    /**
     * 设置数据展示
     *
     * @param bean
     */
    private void setData(PeopleInfoBean bean) {
        tvHeaderName.setText(bean.getPeople().getName() + "·" + bean.getPeople().getRefCode());
        tvHeaderBumen.setText(bean.getPeople().getDeptName() + "·" + bean.getPeople().getPositionName());

        MyUtils.displayUserHeader(PeopleInfoActivity.this, tvHeaderImg, bean.getPeople().getHeadImg(), bean.getPeople().getName());
        tvBumen.setText(TextUtils.isEmpty(bean.getPeople().getDeptBranc()) ? "无" : bean.getPeople().getDeptBranc());
        tvShangji.setText(TextUtils.isEmpty(bean.getPeople().getManagerName()) ? "无" : bean.getPeople().getManagerName());
        tvShouji.setText(TextUtils.isEmpty(bean.getPeople().getPhone()) ? "无" : bean.getPeople().getPhone());
        tvZuoji.setText(TextUtils.isEmpty(bean.getPeople().getTelephone()) ? "无" : bean.getPeople().getTelephone());
        tvChuanzhen.setText("无");
        tvEmail.setText(TextUtils.isEmpty(bean.getPeople().getEmali()) ? "无" : bean.getPeople().getEmali());
        tvHRBP.setText(TextUtils.isEmpty(bean.getPeople().getHrbp()) ? "无" : bean.getPeople().getHrbp());
        tvDizhi.setText(TextUtils.isEmpty(bean.getPeople().getAddress()) ? "无" : bean.getPeople().getAddress());
        isContains = bean.getIsContains();
        chatName = bean.getPeople().getAccount();
        realName = bean.getPeople().getName();
        headerImg = bean.getPeople().getHeadImg();
    }


    /**
     * 添加到常用联系人
     *
     * @param refCode
     * @param otherLoginName
     */
    private void addAction(String refCode, String otherLoginName) {
        Map<String, String> map = new HashMap<>();
        map.put("userCode", refCode);
        map.put("topContacts", otherLoginName);
        mRxManager.add((Api.getDefault().addContact(Api.newRequestBody(map)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<MessageInfoBean>(PeopleInfoActivity.this, true) {
                    @Override
                    protected void _onNext(MessageInfoBean messageInfoBean) {
                        if (null == messageInfoBean) {
                            ToastUitl.showShort("数据有误");
                            return;
                        }
                        ToastUitl.showShort(messageInfoBean.getData().toString());
                        isContains = "1";
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUitl.showShort("请求失败" + message);
                    }
                }));
    }


    /**
     * 移除到常用联系人
     *
     * @param refCode
     * @param otherLoginName
     */
    private void deleteAction(String refCode, String otherLoginName) {
        Map<String, String> map = new HashMap<>();
        map.put("userCode", refCode);
        map.put("topContacts", otherLoginName);
        mRxManager.add((Api.getDefault().deleteContact(Api.newRequestBody(map)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<MessageInfoBean>(PeopleInfoActivity.this, true) {
                    @Override
                    protected void _onNext(MessageInfoBean messageInfoBean) {
                        if (null == messageInfoBean) {
                            ToastUitl.showShort("数据有误");
                            return;
                        }
                        ToastUitl.showShort(messageInfoBean.getData().toString());
                        isContains = "0";
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUitl.showShort("请求失败" + message);
                    }
                }));
    }
}
